package com.lottery.im.module.chat.presenter;

import android.app.Activity;
import com.lottery.im.base.BaseFragmentPresenter;
import com.lottery.im.module.chat.view.UserProfileView;
import com.tencent.imsdk.TIMFriendshipManager;
import com.tencent.imsdk.TIMUserProfile;
import com.tencent.imsdk.TIMValueCallBack;
import com.tencent.imsdk.ext.sns.TIMFriendResult;
import com.tencent.imsdk.ext.sns.TIMFriendshipManagerExt;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.FragmentEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class UserProfilePresenter extends BaseFragmentPresenter<UserProfileView> {

  public UserProfilePresenter(Activity activity,
      UserProfileView iView,
      LifecycleProvider<FragmentEvent> provider) {
    super(activity, iView, provider);
  }

  public void getUsersProfile(String id) {
    List<String> ids = new ArrayList<>();
    ids.add(id);
    TIMFriendshipManager.getInstance().getUsersProfile(ids,
        new TIMValueCallBack<List<TIMUserProfile>>() {
          @Override public void onError(int i, String s) {

          }

          @Override public void onSuccess(List<TIMUserProfile> timUserProfiles) {
            if (null != timUserProfiles && timUserProfiles.size() > 0) {
              if (null == iView) return;
              iView.refreshUsersProfile(timUserProfiles.get(0));
            }
          }
        });
  }

  public void addBlack(String id) {
    iView.showLoadingDialog();
    TIMFriendshipManagerExt.getInstance().addBlackList(Collections.singletonList(id),
        new TIMValueCallBack<List<TIMFriendResult>>() {
          @Override public void onError(int i, String s) {
            if (null == iView) return;
            iView.cancelLoadingDialog();
            iView.showToast("添加黑名单失败,请稍后再试");
            iView.addBlackFail();
          }

          @Override public void onSuccess(List<TIMFriendResult> timFriendResults) {
            if (null == iView) return;
            iView.cancelLoadingDialog();
            iView.showToast("添加黑名单成功");
            iView.addBlackSuc();
          }
        });
  }
}
